package org.dxw.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.dxw.common.enums.TableChangeEnum;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author dxw
 * @Date 2023/11/21 22:54
 * @Description
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TableChangeMsg {

    private String msgUUID;

    /**
     * 表的实体类，可以为空，为空表示表数据发生了变动，但不知道是哪些记录
     */
    private List<Object> rowDOList;

    /**
     * 表名
     */
    private String tableName;

    /**
     * 操作类型，新增，修改，删除
     */
    private String changeType;


    /**
     * 成功消费这条消息的消费者
     */
    private List<String> sucessConsumerList;

    /**
     * 失败消费这条消息的消费者
     */
    private List<FailConsumer> failConsumerList;

    public void addSuccessConsumerList(String name){
        if(sucessConsumerList == null){
            sucessConsumerList = new LinkedList<>();
        }
        sucessConsumerList.add(name);
    }

    public void consumerFail(TableChangeEnum tableChangeEnum, String failReason){
        if(failConsumerList == null){
            failConsumerList = new LinkedList<>();
        }
        FailConsumer failConsumer = null;
        for (FailConsumer failConsumer1 : failConsumerList) {
            if(tableChangeEnum.getName().equals(failConsumer1.getName())){
                failConsumer = failConsumer1;
                break;
            }
        }
        if(failConsumer == null) failConsumer = new FailConsumer();
        failConsumer.setFailReason(failReason);
        failConsumer.setName(tableChangeEnum.getName());
        failConsumer.setFailCount(failConsumer.getFailCount()+1);
        failConsumerList.add(failConsumer);
    }



    @Data
    public static class FailConsumer{

        private String name;

        private String failReason;

        private int failCount;

    }

}
